package org.sgx.gwteditors.client.impl1.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.basetypes.Time;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * simple editor for datatype Time. TODO: events. 
 * @author sg
 *
 */
public class TimeEditor extends Grid implements Editor<Time> {

	private IntegerBox hour;
	private IntegerBox minute;
	private IntegerBox second;
	private Time model;

	@Override
	public boolean isSimple() {
		return true;
	}
	
	public TimeEditor() {
		super(1,5);
		hour = new IntegerBox();
		hour.setWidth("20px");
		minute = new IntegerBox();
		minute.setWidth("20px");
		second = new IntegerBox(); 
		second.setWidth("20px");
		
		setWidget(0, 0, hour); 
		setWidget(0, 1, new Label(":"));
		
		setWidget(0, 2, minute); 
		setWidget(0, 3, new Label(":"));
		
		setWidget(0, 4, second); 
		
//		addValueChangeHandler(this);
	}

	@Override
	public Time flush() {
		if(model==null)
			return null; //TODO: flush() before load() error.
		model.setHour(hour.getValue()); 
		model.setMinute(minute.getValue());
		model.setSecond(second.getValue()); 
		return model;
	}

	@Override
	public void load(Time t) {
		this.model = t;
		hour.setValue(t.getHour());
		minute.setValue(t.getMinute()); 
		second.setValue(t.getSecond()); 
	}
	@Override
	public Widget getWidget() {
		return this;
	}
	
	@Override
	public void addValueChangeListener(ValueChangeListener<Time> l) {
		EditorEventManager.<Time>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<Time> l) {
		EditorEventManager.<Time>getInstance().removeValueChangeListener(this, l);
	}

//	@Override
//	public void onValueChange(ValueChangeEvent<Time> event) {
//		EditorEventManager.<Time>getInstance().fireValueChangeGwtEvent(this, event);	
//	}

	@Override
	public List<EditorError<Time>> getErrors() {
		return EditorErrorManager.<Time>getInstance().getErrors(this);
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_TIME}; 
	}
}
